/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gracefulmm.dealImage;

/**
 *
 * @author wenbo
 */
import com.taobao.api.FileItem;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.PictureCategory;
import com.taobao.api.request.PictureCategoryAddRequest;
import com.taobao.api.request.PictureUploadRequest;
import com.taobao.api.response.PictureCategoryAddResponse;
import com.taobao.api.response.PictureUploadResponse;
import java.util.Date;
import org.apache.log4j.Logger;

public class TaobaoDealUtil {
    
    private static final Logger log = Logger.getLogger(TaobaoDealUtil.class);

    protected static Date updateSessionKeyTime = null;
    
    
    protected static TaobaoClient taobaoClient = null;
    
    protected static final String SESSION_KEY = "6101004f25c61b6ea560ea75f3a2977c654316832b3ae49892613908";

    public static boolean uploadImg(TaobaoClient client,String sessionKey,Long pictureCategoryId,String filePath,String title,String inputTitle) {
        try {
            log.info("准备上传文件到图片空间，文件名为："+filePath);
            PictureUploadRequest req = new PictureUploadRequest();
            req.setImageInputTitle(inputTitle);
            req.setImg(new FileItem(filePath));
            req.setTimestamp(new Date().getTime());
            req.setPictureCategoryId(pictureCategoryId);
            req.setTitle(title);
            PictureUploadResponse rsq = client.execute(req,sessionKey);
            if (rsq.isSuccess()) {
                log.info("上传文件到图片空间成功，文件名为："+filePath);
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            log.error("上传图片到淘宝出现问题!", ex);
        }
        return false;
    }
    
    public static Long AddPictureCategory(TaobaoClient client,String sessionKey,String pictureCategoryName){
//        TaobaoClient client = new DefaultTaobaoClient(url, appkey, appSecret);//实例化TopClient类
        log.info("准备添加图片类目，类目名为："+pictureCategoryName);
        PictureCategoryAddRequest request = new PictureCategoryAddRequest();
        request.setPictureCategoryName(pictureCategoryName);
        request.setTimestamp(new Date().getTime());
        try {
            PictureCategoryAddResponse response = client.execute(request,sessionKey);
            PictureCategory category = null;
            if(response.isSuccess()){
                category = response.getPictureCategory();
//                System.out.println("id:"+category.getPict
                        log.info("添加图片类目成功，类目id为："+category.getPictureCategoryId());
            }else{
//                System.out.println(response.getBody());
                log.info("添加图片类目失败!");
            }
            if(category == null){
                return null;
            }
            return category.getPictureCategoryId();
        } catch (Exception ex){ 
            log.error("添加图片类目出现问题!", ex);
        }
        return null;
    }
    
//    public static void findImgByCategoryId() {
//        try {
//            //         ITopClient client = new DefaultTopClient(url, appkey, appsecret);
//            TaobaoClient client = new DefaultTaobaoClient(url, appkey, appSecret);//实例化TopClient类
//            PictureGetRequest req = new PictureGetRequest();
//            req.setPictureCategoryId(0l);
//            req.setPageSize(10l);
//            req.setPageNo(1l);
//            PictureGetResponse rep = client.execute(req, sessionKey);
//            System.out.println(rep.getBody());
//            List<Picture> pictures = rep.getPictures();
//            for(Picture picture:pictures){
//                System.out.println("id:"+picture.getPictureId());
//                System.out.println("title:"+picture.getTitle());
//            }
//        } catch (Exception ex) {
//            Logger.getLogger(TaobaoDealUtil.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
    
//    public static void getPictureCategoryInfo() {
//        try {
//            //         ITopClient client = new DefaultTopClient(url, appkey, appsecret);
//            TaobaoClient client = new DefaultTaobaoClient(url, appkey, appSecret);//实例化TopClient类
//            PictureCategoryGetRequest req = new PictureCategoryGetRequest();
//            PictureCategoryGetResponse response = client.execute(req,sessionKey);
//            List<PictureCategory> pictureCategories =  response.getPictureCategories();
//            System.out.println(response.getBody());
//            for(PictureCategory pics:pictureCategories){
//                System.out.println("id:"+pics.getPictureCategoryId()+",name:"+pics.getPictureCategoryName());
//            }
//        } catch (ApiException ex) {
//            Logger.getLogger(TaobaoDealUtil.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }

    public static void main(String[] args) {
        TaobaoClient taobaoClient = ImageTools.getTaobaoClient();
        TaobaoDealUtil.AddPictureCategory(taobaoClient,"6101004f25c61b6ea560ea75f3a2977c654316832b3ae49892613908","测试");
    }
}
